#ifndef _TemplateProc_H_
#define _TemplateProc_H_


#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set>

#include <boost/cregex.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/xpressive_fwd.hpp>


#include "template_match.h"

using namespace boost;

using namespace std;
using std::wstring;
#define _GLIBCXX_USE_WSTRING

typedef enum HistoryState
{
	HISTORY_READY=0,
	HISTORY_DELETE
};



class TemplateEntry
{
	
public:
	TemplateEntry(const char* template_entry_) :template_entry_(template_entry_){
		PreprocessTemplate();
	};
	~TemplateEntry(){};

	inline xpressive::wcregex& GetTemplateRegex()
	{
		return template_regex_;
		
	}
	inline std::string GetTemplateText()
	{
		return template_entry_;
	}
	int PreprocessTemplate();
	inline std::vector<std::string>& GetKeys()
	{
		return keys_;
	}
protected:
	xpressive::wcregex& CompileTemplate(const std::string& regex_part);

private:
	int field_num_;
	xpressive::wcregex template_regex_;
	std::string template_entry_;
	std::vector<std::string> keys_;

};



class TemplateProc
{
public:
	TemplateProc();
	~TemplateProc();

	int Init(const std::string& template_file);
	int Process(const UserCommand* user_cmd, UserCommandInfo* user_cmd_info);
	int Stop();
	
	HistoryState GetHistoryState();
	int GetHistoryInfo(const std::string& user_id);
	float ComputeSingerConfidence(const std::string& ner);
	float DoNERClassification(const std::string& ner);
protected:
	int ProcessInternal(std::string& user_command, UserCommandInfo* user_cmd_info);

private:
	HistoryState state_;
	std::vector<std::string> template_entries_;
	std::vector<TemplateEntry> compliled_templates_;

};


std::string wstring2string(const std::wstring& ws);
std::wstring string2wstring(const std::string& s);
string Wstring2String(wstring sToMatch);
wstring String2Wstring(string sToMatch);
int LoadFile(char* inputfile);
void DoRegexMatch(const std::string& template_str, const std::string& command_str);
bool DoMatch(const std::string& template_str, const std::string& command_str, std::vector<std::string>& results);






#endif
